package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.cache.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.permission.RequireLogin;
import cn.wolfcode.wolf2w.permission.UserParam;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {


    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/detail")
    private Object detail(String id) {
        Strategy strategy = strategyService.get(id);

        //攻略阅读数 +1
        strategyStatisVORedisService.viewnumIncrease(id, 1);
        return JsonResult.success(strategy);
    }

    @GetMapping("/query")
    private Object query(StrategyQuery qo) {

        return JsonResult.success(strategyService.query(qo));
    }

    //查询主题
    @GetMapping("/themes")
    private Object themes() {

        return JsonResult.success(strategyThemeService.list());
    }


    // 添加评论
    @RequireLogin
    @PostMapping("/addComment")
    private Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo) {
        //属性拷贝方法，将当前登录用户的属性数据拷贝到评论中
        //参数1：源对象；参数2：目标对象
        //前提：属性名字必须一样才可以进行拷贝
        BeanUtils.copyProperties(userInfo, strategyComment);
        //用户id跟评论字段不一样，需要另外进行设置
        strategyComment.setUserId(userInfo.getId());
        strategyCommentService.save(strategyComment);

        //攻略评论数 +1
        strategyStatisVORedisService.replynumIncrease(strategyComment.getStrategyId(), 1);
        return JsonResult.success();
    }

    //评论列表，分页
    @GetMapping("/comments")
    private Object comments(StrategyCommentQuery qo) {
        return JsonResult.success(strategyCommentService.query(qo));
    }

    //评论点赞
    @RequireLogin
    @PostMapping("/commentThumb")
    private Object commentThumb(String cid, @UserParam UserInfo userInfo) {

        strategyCommentService.commentThumb(cid, userInfo.getId());
        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    private Object statisVo(String sid) {
        StrategyStatisVO vo = strategyStatisVORedisService.getStrategyStatisVO(sid);
        return JsonResult.success(vo);
    }

    //攻略收藏
    @RequireLogin
    @PostMapping("/favor")
    private Object favor(String sid, @UserParam UserInfo userInfo) {

        boolean ret = strategyStatisVORedisService.favor(sid, userInfo.getId());
        return JsonResult.success(ret);
    }
    //攻略顶caoz
    @RequireLogin
    @PostMapping("/strategyThumbup")
    private Object strategyThumbup(String sid, @UserParam UserInfo userInfo) {

        boolean ret = strategyStatisVORedisService.strategyThumbup(sid, userInfo.getId());
        return JsonResult.success(ret);
    }
}
